package com.yylending.remotevideo;

import android.content.Context;
import com.bairuitech.anychat.*;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yylending.remotevideo.anychat.BaseEvent;
import com.yylending.remotevideo.anychat.ObjectEvent;
import com.yylending.remotevideo.anychat.VideoCallEvent;

/**
 * Created by HenryZhang on 2017/4/12.<br>
 * Copyright © 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class AnyChatPresenter extends Presenter {
	private static final String TAG = AnyChatPresenter.class.getSimpleName();

	private final int LOCALVIDEOAUTOROTATION = 1; // 本地视频自动旋转控制

	/**
	 * 设置基本回调事件接收
	 */
	private BaseEvent mBaseEvent;
	/**
	 * 设置业务对象回调事件接收
	 */
	private ObjectEvent mObjectEvent;
	private VideoCallEvent mVideoCallEvent;

	public int mCallingUserId;

	public String getUserName(int userId) {
		return anychat.GetUserName(userId);
	}

	public int getWaitingSecond() {
		return AnyChatCoreSDK.ObjectGetIntValue(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, mObjectEvent.queueId, AnyChatObjectDefine.ANYCHAT_QUEUE_INFO_WAITTIMESECOND);
	}

	public static class Generator implements PresenterFactory<AnyChatPresenter> {
		@Override
		public AnyChatPresenter create() {
			return new AnyChatPresenter();
		}
	}

	public interface View extends IView {
		void anyChatInitializeComplete();

		/**
		 * OnAnyChatConnectMessage、OnAnyChatLinkCloseMessage和OnAnyChatLoginMessage都会回调
		 * @param errorCode 0表示登录成功，其他均失败，如果是OnAnyChatConnectMessage返回失败，AnyChat会自动重连
		 */
		void loginResult(int errorCode);
		void enterRoomResult(int roomId);

		/**
		 * @param num   当前排队人数
		 * @param front 队列当前用户前面的用户数
		 */
		void roomUserNum(int num, int front);
		void remoteVideoCalling(int userId);
		void remoteVideoStarting(int userId, int roomId);
		void callingCanceled();
	}

	private View mView;
	private AnyChatCoreSDK anychat = null;

	@Override
	public void attach(IView view) {
		mView = (View) view;
	}

	@Override
	public void detach() {
		mView = null;
	}

	public void releaseAnyChat() {
//		anychat.SetBaseEvent(null);
//		mBaseEvent.release();
//		mBaseEvent = null;
//
//		anychat.SetObjectEvent(null);
//		mObjectEvent.release();
//		mObjectEvent = null;
//
//		anychat.SetVideoCallEvent(null);
//		mVideoCallEvent.release();
//		mVideoCallEvent = null;

		anychat.Logout();
		anychat.Release();
	}

	public void initAnyChat(Context ctx) {
		if (anychat == null)
			anychat = AnyChatCoreSDK.getInstance(ctx);
		mBaseEvent = new BaseEvent(mView, anychat);
		anychat.SetBaseEvent(mBaseEvent);

		mObjectEvent = new ObjectEvent(mView);
		anychat.SetObjectEvent(mObjectEvent);

		mVideoCallEvent = new VideoCallEvent(mView);
		anychat.SetVideoCallEvent(mVideoCallEvent);

		anychat.InitSDK(android.os.Build.VERSION.SDK_INT, 0);
		AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_AUTOROTATION, LOCALVIDEOAUTOROTATION);
		//官方Demo个别例子没有调用下面的方法
		AnyChat.applyVideoConfig(ctx);
		anychat.mSensorHelper.InitSensor(ctx.getApplicationContext());
		AnyChatCoreSDK.mCameraHelper.SetContext(ctx.getApplicationContext());
		mView.anyChatInitializeComplete();
	}

	/**
	 * 登入AnyChat云服务器
	 *
	 * @param nickName
	 * @param contractNO
	 * @param timestamp
	 * @param sign
	 */
	public void login(String nickName, String contractNO, long timestamp, String sign) {
		anychat.Connect(Global.ANY_CHAT_HOST, Global.ANY_CHAT_PORT);
		anychat.LoginEx(nickName, -1, contractNO, Global.ANY_CHAT_APP_ID, (int) timestamp, sign, "");//Next Step: OnAnyChatLoginResult
//		anychat.SetSDKOptionString(AnyChatDefine.BRAC_SO_CLOUD_APPGUID, "C782383B-FC3C-4726-815B-9D6117BF681A");// 设置应用Id
//		anychat.Connect("cloud.anychat.cn", 8906);// 连接服务器
//		anychat.Login("AnyChat123", "");//登录服务器
	}

	/**
	 * 开始排队
	 */
	public void incomingQueue() {
		AnyChatCoreSDK.ObjectControl(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, mObjectEvent.queueId, AnyChatObjectDefine.ANYCHAT_QUEUE_CTRL_USERENTER, 0, 0, 0, 0, "");
	}

	public void quitQueue() {
		AnyChatCoreSDK.ObjectControl( AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_QUEUE, mObjectEvent.queueId, AnyChatObjectDefine. ANYCHAT_QUEUE_EVENT_LEAVERESULT, 0, 0, 0,0, "");
	}

	public void reconnection() {
	}

	public void handleCalling(int userId, int event) {
		anychat.VideoCallControl(AnyChatDefine.BRAC_VIDEOCALL_EVENT_REPLY, userId, event, 0, 0, "");
//		anychat.VideoCallControl(dwEventType, dwUserId, dwErrorCode, dwFlags, dwParam, szUserStr);
	}
}
